package IOperator;

import Book.*;
import FileOperation.saveBooks;
import java.sql.SQLException;
import java.util.Scanner;
public class BorrowBooks implements IOperator {

    @Override
    public void work(Bookshelf bookshelf) throws SQLException {
        System.out.println("借阅图书!");
        System.out.print("请输入你要借阅的图书# ");
        Scanner scanner = new Scanner(System.in);
        String bookName = scanner.nextLine();

        int currCount = bookshelf.getBooksNumber();
        for (int i = 0; i < currCount; i++) {
            Book book = bookshelf.getBooks(i);
            if (bookName.equals(book.getName())) { //找到了这本书
                //判断这本书是否被借阅
                if (book.isBorrow() == true) {
                    System.out.println("《" + book.getName() + "》已经被借阅!");
                    return;
                } else {
                    book.setBorrow(true);
                    new saveBooks().fileSaveCover(bookshelf);
                    System.out.println("成功借阅到《" + book.getName() + "》!");
                    return;
                }
            }
        }

        System.out.println("没有你要借阅的书籍!");
    }
}
